﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Core.Utility.Extendsions.Validation
{
    /// <summary>
    /// 返回的错误集合类
    /// </summary>
    public class ValidationExtensionsResult
    {
        /// <summary>
        /// 错误成员列表
        /// </summary>
        public List<ErrorMember>? ErrorMembers { get; set; }

        /// <summary>
        /// ErrorMembers 中所有 ErrorMember 的拼接
        /// </summary>
        public string ErrorMessages
        {
            get
            {
                return GetErrorMessages();
            }
        }

        /// <summary>
        /// 获取错误信息
        /// </summary>
        /// <param name="joinString">两个错误信息之间拼接的字符串</param>
        /// <param name="endString">每个错误信息结尾拼接的字符串</param>
        /// <returns></returns>
        public string GetErrorMessages(string joinString = "\r\n", string endString = ";")
        {
            List<string> messageList = new List<string>();
            if(ErrorMembers != null)
            {
                ErrorMembers.ForEach(member =>
                {
                    if (!string.IsNullOrEmpty(member.ErrorMessage))
                    {
                        messageList.Add($"{member.ErrorMessage}{(member.ErrorMessage.EndsWith(Environment.NewLine) ? "" : endString)}");
                    }
                });
            }
            return string.Join(joinString, messageList);
        }

        /// <summary>
        /// 是否有效，有效返回true，无效返回false
        /// </summary>
        public bool IsValid { get; set; }

    }

    /// <summary>
    /// 返回的错误信息成员
    /// </summary>
    public class ErrorMember
    {
        public string? ErrorMessage { get; set; }
        public string? ErrorMemberName { get; set; }
    }
}
